package com.ruoyi.framework.aop;

import com.ruoyi.common.exception.EquipmentException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.project.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.transaction.TransactionException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
/**
 * @功能描述 aop解析注解
 * @date 2018-11-02
 */
@Slf4j
public class UserRepeatSubmitAop {

    @Autowired
    private RedisUtils redisUtils;


    @Before("@annotation(com.ruoyi.framework.aop.UserRepeatSubmit)")
    public void before(JoinPoint pjp) throws TransactionException {
        //ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = ServletUtils.getRequest().getHeader("token");
        //不存在直接跳过拦截
        if(!ObjectUtils.isEmpty(token)){
            String key="system:front:user:"+token;
            String loginOut=redisUtils.getObjectKey(key);
            if(!ObjectUtils.isEmpty(loginOut) && loginOut.equals("loginout")){
                throw new EquipmentException("账户在另一台设备登陆,请重新登陆");
            }
        }
    }

}
